import { req } from '@/util/request'
import type { Page, PageOprion, ResData } from '@/types/index'
import type { RoleMenusType, RoleTable } from '@/types/role'
import { deepCopy } from '@/util/token'

// 查询
export function getAllRole(page: PageOprion, cdn: RoleTable = {}): ResData<Page<RoleTable>> {
  cdn = deepCopy(cdn)
  return req.post('/role/get/all', {
    num: page.num,
    size: page.size,
    cdn
  })
}

// 添加角色
export function addRole(data: RoleTable): ResData<any> {
  return req.post('/role/add', data)
}

//修改角色
export function updateRole(data: RoleTable): ResData<any> {
  data = deepCopy(data)
  return req.post('/role/update', data)
}

// 删除角色
export function delRole(ids: number[]): ResData<any> {
  return req.post('/role/delete', ids)
}

//获取角色拥有的接口权限
export function getRoleHandle(rid: number): ResData<number[]> {
  return req.get('/role/get/handles', { params: { rid } })
}

// 更新角色的接口权限
export function updateRoleHandle(rid: number, data: number[]): ResData<any> {
  return req.post('/role/handles_update', { rid, data })
}

export function getRoleMenus(rid: number): ResData<RoleMenusType> {
  return req.get('/role/get/menus', {
    params: {
      rid
    }
  })
}

export function updateRoleMenus(rid: number, mids: number[], tags: string[]): ResData<any> {
  return req.post('/role/update/menus', {
    rid,
    mids,
    tags
  })
}
